/*
    页面名称：员工 -图书检索页面
    作者: 苗怀雨
    时间:2017年12月31日
    最后编辑：2017年12月31日20:20:13
*/

$(document).ready(function() {
    /* 检索关键字类型改变 */
    var selects = $("select.form-control");
    var $input1 = $("<input type='text' name='author'>");
    var $input2 = $("<input type='text' name='publish'>");

    $input1.css("display","none");
    $input1.appendTo("form");
    $input2.css("display","none");
    $input2.appendTo("form");

    selects.eq(0).change(function () {
        if(selects.eq(0).val() == 0){
            $("input").attr("name","bookName");
            $input1.attr("name","author");
            $input2.attr("name","publish");
        }else if(selects.eq(0).val() == 1){
            $("input").attr("name","author");
            $input1.attr("name","bookName");
            $input2.attr("name","publish");
        }else{
            $("input").attr("name","publish");
            $input1.attr("name","author");
            $input2.attr("name","bookName");
        }
    });

    /*检索按钮*/
    $("#search").click(function () {
        var bookName = $("input[name='bookName']").val();
        var author = $("input[name='author']").val();
        var bookClass = $(selects.eq(1).val());

        if($(this).parents("span").siblings("input.form-control").val() == ""){
            createPNotify("提示信息","error","请输入关键字");
            return;
        }
        $("#search").text("");
        $("#search").append($("<i class='fa fa-spinner'></i>"));

        $("form").submit();
    });

    var $hotSearch = $("#hotSearch");
    var $matchSearch = $("#matchSearch");
    var $searchInput =  $("div.top_search").children("div").children("input");
    //focus事件:搜索框显示热搜
    $searchInput.focus(function () {
        $hotSearch.css("display","block");
    });
    $searchInput.blur(function () {
        setTimeout(function () {
            $hotSearch.css("display","none");
            $matchSearch.css("display","none");
        },200)
    });
    $searchInput.bind("input",function () {
        if($searchInput.val().length == 0){
            $hotSearch.css("display","block");
            $matchSearch.css("display","none");
        } else{
            $("#matchSearch").children("li").remove();
            matchKey($searchInput.val());
            $hotSearch.css("display","none");
            $matchSearch.css("display","block");
        }
    });
});

$(document).keydown(function(event){
    searchBook(event);
});

/* 查看图书按钮*/
function bookInfo(bookId){
    window.location.href = "bookInfoPage?bookId=" + bookId;
}

/*函数:回车登录*/
function searchBook(event) {
    if(event.keyCode == 13){    //按下回车
        $("#search").trigger("click");
    }
}

function toSearchByMatchKey(e) {
    $("#search").parent().siblings("input").val($(e).text());
    $("#search").trigger("click");
}

function toSearchByHotKey(e) {
    var length = $(e).children("span").text().length;
    $("#search").parent().siblings("input").val($(e).text().substr(length));
    $("#search").trigger("click");
}
